package cn.com.hhrcw.job;

import cn.com.hhrcw.entity.OpInterviewHistory;
import cn.com.hhrcw.service.IOpInterviewHistoryService;
import cn.com.hhrcw.utils.RedisLockUtils;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 面试开始状态设置
 *
 * @author lixingxing
 */
@Component
@Slf4j
public class InterviewStartTask implements Job {
    @Autowired
    private IOpInterviewHistoryService opInterviewHistoryService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        DateTime date = DateUtil.date();
        try {
            if (RedisLockUtils.tryLock("INTERVIEW_START_TASK", 120)) {
                opInterviewHistoryService.lambdaUpdate()
                    .eq(OpInterviewHistory::getDelFlag, false)
                    .eq(OpInterviewHistory::getOutLineState, 1)
                    .eq(OpInterviewHistory::getTime, date.toString(DatePattern.NORM_DATETIME_PATTERN))
                    .set(OpInterviewHistory::getOnLineState, 2)
                    .update();
            }
        } finally {
            RedisLockUtils.unLock("INTERVIEW_START_TASK");
        }
    }
}
